package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 广告位管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_advpos")
public class AdvposController {
    @Autowired
    private AdvposService advposService;

    /**
     * 广告位列表-分页
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("list")
    public ResultData list(String advposdesc, String advposname, Integer page, Integer limit) {
        log.info("广告位描述advposdesc:{},广告位名称advposname:{},页码page:{},每页数量limit:{}",
                advposdesc, advposname, page, limit);
        HashMap<String, Object> map = advposService.getPageHashMap(advposdesc, advposname, page, limit);
        return ResultData.ok("", map);
    }


    /**
     * 广告位修改
     *
     * @param advpos
     * @return
     */
    @PostMapping("update")
    public ResultData update(@RequestBody Advpos advpos) {
        log.info("广告位修改Advpos:{}", advpos);
        advposService.updateById(advpos);
        return ResultData.ok("", "广告位修改成功");
    }

    /**
     * 广告位删除
     *
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResultData del(Integer id) {
        log.info("广告位删除id:{}", id);
        advposService.removeById(id);
        return ResultData.ok("", "广告位删除成功");
    }

    /**
     * 广告位列表-全部
     *
     * @return
     */
    @GetMapping("all")
    public ResultData all() {
        log.info("广告位列表-全部");
        List<Advpos> list = advposService.list();
        return ResultData.ok("", list);
    }

    /**
     * 返回广告位详情
     *
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id) {
        log.info("返回广告位详情id:{}", id);
        Advpos advpos = advposService.getById(id);
        return ResultData.ok("", advpos);
    }

    /**
     * 广告位添加
     *
     * @param advpos
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Advpos advpos) {
        log.info("广告位添加Advpos:{}", advpos);
        advpos.setDefault_data("1");
        advposService.save(advpos);
        return ResultData.ok("", "广告位添加成功");
    }

}
